﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SoftHotel.Models;
using System.Data.Entity;
using System.Data;
using PagedList;

namespace SoftHotel.Controllers
{
    public class PerfilController : Controller
    {
        //
        // GET: /Perfil/
        private PerfilDAO dbPerfil = new PerfilDAO();
        private PerfilDAO dbPerfil2 = new PerfilDAO();

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(Perfil perfil, string button)
        {
            if (button == "Buscar")
            {
                return RedirectToAction("Buscar", perfil); //Model de Buscar es IEnumerable (*)
            }

            return RedirectToAction("Create");
        }

        public ActionResult Buscar( int? page)
        {

            int tamanhoPagina = 3;
            int NumPagina = (page ?? 1);

            var ents = from c in dbPerfil.entidades select c;
            var perfiles = from d in dbPerfil.perfiles select d;
            perfiles = perfiles.OrderBy(s => s.nombre.ToLower());
           // return View(perfiles.ToList());
            return View(perfiles.ToPagedList(NumPagina, tamanhoPagina));
        }
        //
        // GET: /Perfil/Details/5

        public ActionResult Details(Perfil perfil)
        {
            //var entXper = dbPerfil.entidaxperfil.Include(a => a.objentidad).Include(a => a.objperfil).Where(a => a.idPerfil == perfil.idperfil);
            var genreModel = dbPerfil.entidaxperfil.Include(a => a.objentidad).Where(a => a.idPerfil == perfil.idperfil);
            ViewBag.perfil = perfil;
            
            //var aux = from c in dbPar.parametros where c.tipo == "ESTADO_CLIENTE" && c.valor == "Activo" select c;
            //return View(entXper.ToList());
            return View(genreModel.ToList());
        }

        [HttpPost]
        public ActionResult Details(List<EntidadPerfil> entidades, object dummy)
        {
            try
            {
                if (ModelState.IsValid)
                {

                    foreach (EntidadPerfil par in entidades)
                    {
                        dbPerfil2.Entry(par).State = EntityState.Modified;
                        dbPerfil2.SaveChanges();
                    }
                    //dbPerfil2.Entry(prueba).State = EntityState.Modified;
                    //dbPerfil2.SaveChanges();

                    //dbPerfil2.SaveChanges();
                    // dbPerfil2.Entry(entXperfil).State = EntityState.Modified;
                    //dbPerfil2.SaveChanges();
                    return RedirectToAction("Index");
                }
                return View();
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Perfil/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Perfil/Create

        [HttpPost]
        public ActionResult Create(Perfil perfil)
        {
            try
            {
                perfil.fechaModificacion = DateTime.Now;
                perfil.estado = 1;
                if (ModelState.IsValid)
                {
                    dbPerfil.perfiles.Add(perfil);
                    dbPerfil.SaveChanges();
                    int a = perfil.idperfil;

                    var aux = from c in dbPerfil.entidades select c;
                    List<EntidadPerfil> lista = new List<EntidadPerfil>();

                    foreach (Entidad par in aux)
                    {
                        EntidadPerfil exp = new EntidadPerfil();
                        exp.idPerfil = perfil.idperfil;
                        exp.idEntidad = par.idEntidad;
                        exp.editar = false;
                        exp.nuevo = false;
                        exp.ver = false;

                        dbPerfil2.entidaxperfil.Add(exp);
                        dbPerfil2.SaveChanges();
                    }

                }
                return RedirectToAction("Index");
            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
            }
            return View(perfil);
        }
        
        //
        // GET: /Perfil/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Perfil/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Perfil/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Perfil/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
